
import matplotlib.pyplot as plt
import re
import numpy as np
import setting



def parse_result(path):
	f=open(path)
	lines = f.readlines()
	f.close()
	for line in lines:
		mat = re.match("traffic_size=(.+)?",line)
		if mat:
			number = int(mat.groups()[0])
			return number


def get_data(prefix="hedera-partial-restart"):
	ret =[]
	for i in np.arange(0.2,2.1,0.2):
		ts = parse_result("result//hedera//%s-%0.1f.txt"%(prefix,i))
		ret.append(ts)
	return ret

# seq = get_data("hedera-seq-80")
# partial = get_data("hedera-part-80")


seq =     [4089, 4367, 4516, 4228, 4149, 3562, 3154, 3664, 3668, 3676]
partial = [4110, 4444, 4600, 4607, 4588, 4645, 4377, 4442, 4359, 4204]

a=550.0

# seq = [seq[i]/a for i in range(6)]
# partial = [partial[i]/a for i in range(6)]

lam = np.arange(0.2,2.1,0.2)

seq = np.array(seq[0:6])/5000.0
partial = np.array(partial[0:6])/5000.0

lam = lam[0:6]

plt.plot(lam,seq,"r*-",label ="Sequential")
plt.plot(lam,partial,"b.-",label =setting.system_name)
plt.xlabel("Frequency($s^{-1}$)")
plt.ylabel("Bandwidth Utilization")
# plt.title("Performance")
plt.grid()
plt.legend()

width = 4.0
ratio = 2.7/4.0
fig = plt.gcf()
fig.set_size_inches(width,width*ratio)
lam = np.arange(0.2,2.1,0.2)

plt.grid(linestyle='--')
if setting.save:
	fig.savefig("figs/infoco/performance.ps",dpi=400,bbox_inches="tight")
	fig.savefig("figs/infoco/performance.pdf",dpi=400,bbox_inches="tight")
plt.show()

# opnumber(sequ[0.05][0],partial[0.05][0], "p=0.05")
# op_ratio(sequ,partial,[0.05,0.2,0.4,0.6])
# p_time(partial, [0.05,0.2,0.4,0.6])

def comp(a,b):
	print(a,b)
	print((a-b)/b)

comp(partial[-1],partial[0])
comp(partial[-1],seq[-1])